package com.waypolice.manager.controller;

import com.waypolice.common.pojo.SearchResult;
import com.waypolice.common.pojo.TaotaoResult;
import com.waypolice.manager.service.SolrAllTextSearchService;
import com.waypolice.manager.service.UpmsUserRoleService;
import com.waypolice.manager.service.UserService;
import com.waypolice.pojo.CmsJudItem;
import com.waypolice.pojo.CmsJudVolume;
import com.waypolice.pojo.CmsSinItem;
import com.waypolice.pojo.UpmsUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SolrAllTextSearchAction
 * @Description TODO 集成solr全文检索
 * @Author Administrator
 * @Date 2019/8/2 000217:26
 */
@Controller
@Api(value = "全文检索", description = "集成solr全文检索")
@RequestMapping(value = "/pcwindow")
public class SolrAllTextSearchAction {

    @Autowired
    private SolrAllTextSearchService solrAllTextSearchService;

    @Autowired
    protected UserService userService;

    @Autowired
    private UpmsUserRoleService upmsUserRoleService;

    @ApiOperation(value = "全文检索")
    @RequestMapping(value = "/toSolrSearch", method = RequestMethod.GET)
    public String index() {
        return "/searchArchive/searchSolr";
    }

    @ApiOperation(value = "Solr全文检索卷件")
    @RequestMapping(value = "/searchSolrByVolOrItemOrSinItem")
    @ResponseBody
    public Object searchSolrByVolOrItemOrSinItem(HttpServletRequest request, HttpServletResponse response,
                                             String searchStr) {
        if(StringUtils.isBlank(searchStr)){
            return null;
        }
        SearchResult newSearchResult = null;
        TaotaoResult result1 = selectUserIsManager();
        if(result1.getStatus() == 200){
            newSearchResult = solrAllTextSearchService.findAllVolumeAndItemBySolr(searchStr);
        }else {
            newSearchResult = solrAllTextSearchService. getUserVolumeAndItemBySolr(searchStr);
        }
        return  newSearchResult;
    }

    public TaotaoResult selectUserIsManager() {
        TaotaoResult result = new TaotaoResult();
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        List<UpmsUser> user = userService.findByName(username);
        result =upmsUserRoleService.findUserRole(user.get(0).getUserId());
        return  result;
    }

}
